<?php

namespace Home\Controller; 
use Common\Controller\CommonController;
use Home\Model\Balance;
use Home\Model\Bill;
use Home\Model\Goods;
use Home\Model\Order;
use Think\Page;
class BalanceController extends CommonController {
	public function index()
    {
        $balance = M('user_balance');
        $username=$_GET['username'];
        if(empty($_GET['p'])){
            $_GET['p']=1;
        }
        $condition="";
        if(!empty($username)){
            $condition="username like '%".$username."%'";
        }
        $list = $balance->where($condition)->order('id desc')->page($_GET['p'].',25')->select();
        $this->assign('list',$list);// 赋值数据集
        $count      = $balance->where($condition)->count();// 查询满足要求的总记录数
        $Page       = new \Think\Pager($count,25);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->showhtml();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign("username",$username);
        $this->display(); // 输出模板
    }

    //添加余额
    public function add($id){
	    $model=new Balance();
        $model=$model->where(['id'=>$id]);
        $form=$model->find();
        if(empty($form)){
            $this->error('未找到相关用户信息',U('/balance/index'));
        }
        if(IS_POST && $model->validate($_POST) && $model->add()){
            if($model->type==1){
                $this->assign('msg',"用户余额添加成功!!!");// 赋值分页输出
            }else{
                $this->assign('msg',"用户余额扣除成功!!!");// 赋值分页输出
            }
            $this->display("iframe/success");
        }else{
            $this->assign('model',$form);
            $this->assign('number',$model->number);
            $this->assign('type',$model->type);
            $this->assign('error',$model->error);
            $this->display();
        }
    }

    //冻结余额和解除冻结余额
    public function frozen($id){
        $model=new Balance();
        $model=$model->where(['id'=>$id]);
        $model->type=3;
        $form=$model->find();
        if(empty($form)){
            $this->error('未找到相关用户信息',U('/balance/index'));
        }
        if(IS_POST && $model->validate($_POST) && $model->frozen()){
            if($model->type==3){
                $this->assign('msg',"冻结余额成功!!!");// 赋值分页输出
            }else{
                $this->assign('msg',"解除冻结余额成功!!!");// 赋值分页输出
            }
            $this->display("iframe/success");
        }else{
            $this->assign('model',$form);
            $this->assign('number',$model->number);
            $this->assign('type',$model->type);
            $this->assign('error',$model->error);
            $this->assign('content',$model->content);
            $this->assign('error_content',$model->error_content);
            $this->display();
        }
    }

    public function log($id){
        $model=new Balance();
        $model=$model->where(['id'=>$id])->find();
        if(empty($model)){
            $this->error('未找到相关用户信息',U('/balance/index'));
        }
        $log = M('user_balance_log');
        if(empty($_GET['p'])){
            $_GET['p']=1;
        }
        $condition="user_id=".$id;
        if(isset($_GET['type'])){
            if($_GET['type']==1 ||  $_GET['type']==0){
                $condition.=" AND type=".$_GET['type'];
            }else{
                $_GET['type']=-1;
            }
        }else{
            $_GET['type']=-1;
        }
        $list = $log->where($condition)->order('id desc')->page($_GET['p'].',25')->select();
        $this->assign('list',$list);// 赋值数据集
        $count      = $log->where($condition)->count();// 查询满足要求的总记录数
        $Page       = new \Think\Pager($count,25);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->showhtml();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign("model",$model);
        $this->assign("type",$_GET['type']);
        $this->display(); // 输出模板
    }
    //账单
    public function bill(){
        $bill = M('user_balance_bill as ubb');
        if(empty($_GET['p'])){
            $_GET['p']=1;
        }
        $condition="1=1 ";
        if(!empty($_GET)){
            $search=$_GET;
            if(!empty($search['username'])){
                $condition.=" AND u.username like '%".$search['username']."%'";
            }
            if(!empty($search['to_username'])){
                $condition.=" AND ut.username like '%".$search['to_username']."%'";
            }
            if(!empty($search['begin_time'])){
                $condition.=" AND ubb.createdtime >= ".strtotime($search['begin_time'])." AND ubb.createdtime<= ".strtotime($search['end_time']);
            }
        }else{
            $search=[
                'username'=>'',
                'to_username'=>'',
                'begin_time'=>'',
                'end_time'=>''
            ];
        }
        $list = $bill->join(C('DB_PREFIX').'user as u on u.id=ubb.user_id')->
        join(C('DB_PREFIX').'user as ut on ut.id=ubb.to_user_id')->where($condition)->field("ubb.*,u.username as username,ut.username as to_username")->
        order('ubb.id desc')->page($_GET['p'].',25')->select();
        $this->assign('list',$list);// 赋值数据集
        $count      = $bill->join(C('DB_PREFIX').'user as u on u.id=ubb.user_id')->
        join(C('DB_PREFIX').'user as ut on ut.id=ubb.to_user_id')->where($condition)->count();// 查询满足要求的总记录数
        $Page       = new \Think\Pager($count,25);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->showhtml();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign("search",$search);
        $this->display(); // 输出模板
    }

    public function addBill(){
        $model=new Bill();

        if(IS_POST && $model->data($_POST)->create()){
            $model->generateSN();
            $data=$model->getData();
            if($model->add()){
                $this->assign('msg',"用户：".$data['user']['username']." 向用户：".$data['touser']['username'].'转账：'.$data['number']."成功！");// 赋值分页输出
                $this->display("iframe/success");
            }
        }
        $this->assign('error',$model->getError());// 赋值分页输出
        $this->assign('model',$model->getData());// 赋值分页输出
        $this->display(); // 输出模板
    }

    //
    public function goods(){
        $balance = M('balance_goods as g');
        $username=$_GET['username'];
        if(empty($_GET['p'])){
            $_GET['p']=1;
        }
        $condition="1=1 ";
        if(!empty($_GET)){
            $search=$_GET;
            if(!empty($search['username'])){
                $condition.=" AND username like '%".$search['username']."%'";
            }
            if(!empty($search['keyword'])){
                $condition.=" AND (goods_sn like '%".$search['keyword']."%' or name like '%".$search['keyword']."%') ";
            }
            if(isset($search['is_valid']) && ($search['is_valid']=="0" || $search['is_valid']=="1" )){
                $condition.=" AND is_valid =".$search['is_valid'];
            }

            if(isset($search['is_shelves']) && ($search['is_shelves']=="0" || $search['is_shelves']=="1" )){
                $condition.=" AND is_shelves =".$search['is_shelves'];
            }
            if(isset($search['type']) && ($search['type']=="0" || $search['type']=="1" )){
                $condition.=" AND type =".$search['type'];
            }
        }else{
            $search=[
                'username'=>'',
                'keyword'=>'',
                'is_valid'=>'',
                'is_shelves'=>'',
                'type'=>'',
                'begin_time'=>'',
                'end_time'=>'',
            ];
        }
        $list = $balance->where($condition)->order('id desc')->page($_GET['p'].',25')->select();
        $this->assign('list',$list);// 赋值数据集
        $count      = $balance->where($condition)->count();// 查询满足要求的总记录数
        $Page       = new \Think\Pager($count,25);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->showhtml();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign("search",$search);
        $this->display(); // 输出模板
    }
    public function addGoods(){
        $model=new Goods();
        $model->type='0';
        $model->is_valid='1';
        $model->is_shelves='1';
        if(IS_POST && $model->data($_POST)->add()){
            $this->assign('msg',"产品添加成功");
            $this->display("iframe/success");
        }
        $this->assign('error',$model->getError());
        $this->assign('model',$model->getData());
        $this->display(); // 输出模板
    }

    public function updateGoods($id){
        $model=new Goods();
        $model->where("id=".$id)->find();
        if(IS_POST && $model->data($_POST)){
            $model->update();
            $this->assign('msg',"产品修改成功");
            $this->display("iframe/success");
        }
        $this->assign('error',$model->getError());
        $this->assign('model',$model->getData());
        $this->display(); // 输出模板
    }



    //账单
    public function order(){
        $bill = M('balance_order as o');
        if(empty($_GET['p'])){
            $_GET['p']=1;
        }
        $condition="1=1 ";
        if(!empty($_GET)){
            $search=$_GET;
            if(!empty($search['keyword'])){
                $condition.=" AND (o.order_sn like '%".$search['keyword']."%' or u.username like '%".$search['keyword']."%') ";
            }


            if(isset($search['payment_type']) && ($search['payment_type']=="0" || $search['payment_type']=="1" )){
                $condition.=" AND o.payment_type =".$search['payment_type'];
            }

            if(isset($search['order_status']) && ($search['order_status']=="0" || $search['order_status']=="1" || $search['order_status']=="2" || $search['order_status']=="3" )){
                $condition.=" AND o.order_status =".$search['order_status'];
            }

            if(isset($search['payment_status']) && ($search['payment_status']=="0" || $search['payment_status']=="1"|| $search['payment_status']=="2" )){
                $condition.=" AND o.payment_status =".$search['payment_status'];
            }
            if(!empty($search['begin_time'])){
                $condition.=" AND o.createdtime >= ".strtotime($search['begin_time'])." AND o.createdtime<= ".strtotime($search['end_time']);
            }
        }else{
            $search=[
                'keyword'=>'',
                'payment_type'=>'',
                'order_status'=>'',
                'payment_status'=>'',
                'begin_time'=>'',
                'end_time'=>''
            ];
        }
        $list = $bill->join(C('DB_PREFIX').'user as u on u.id=o.user_id')->where($condition)->field("o.*,u.username as username")->
        order('o.id desc')->page($_GET['p'].',25')->select();
        $this->assign('list',$list);// 赋值数据集
        $count      = $bill->join(C('DB_PREFIX').'user as u on u.id=o.user_id')->where($condition)->count();// 查询满足要求的总记录数
        $Page       = new \Think\Pager($count,25);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->showhtml();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign("search",$search);
        $this->display(); // 输出模板
    }


    public function updateOrder($id){
        $model=new Order();
        if(empty($model->where(['id'=>$id])->find())){
            $this->error('未找到相关用户信息',U('/balance/order'));
        }
        if(IS_POST && $model->data($_POST)){
            if($model->update()){
                $this->assign('msg',"订单取消成功");
                $this->display("iframe/success");
            }
        }

        $balance = M('balance_order_log');
        $list = $balance->where("order_id=".$model->id)->select();

        $this->assign('list',$list);
        $this->assign('error',$model->getError());

        $this->assign('model',$model->getData());
        $this->display();
    }
}